package interview

import (
	"data/config"
	"data/view/approval"
	"data/view/common"
	"public/mysqldb"
	"strconv"
	"strings"
	"time"
)

//创建出差面试单
func CreateInterviewSheet(apply_no string, interviewer, assistant []string, content map[string]approval.Widget_info) (b bool, i_no string) {
	b = true

	if len(content) > 0 {
		var db mysqldb.MySqlDB
		defer db.OnDestoryDB()
		orm := db.OnGetDBOrm(config.GetDbUrl())

		i_no = common.CreateUnique("")

		var sheet Interview_sheet_tbl
		if len(strings.Split(content["出差区域"].Value, common.SPLIT)) == 1 {

			sheet.Province = strings.Split(content["出差区域"].Value, common.SPLIT)[0]

		} else if len(strings.Split(content["出差区域"].Value, common.SPLIT)) == 2 {

			sheet.Province = strings.Split(content["出差区域"].Value, common.SPLIT)[0]
			sheet.City = strings.Split(content["出差区域"].Value, common.SPLIT)[1]

		} else if len(strings.Split(content["出差区域"].Value, common.SPLIT)) == 3 {

			sheet.Province = strings.Split(content["出差区域"].Value, common.SPLIT)[0]
			sheet.City = strings.Split(content["出差区域"].Value, common.SPLIT)[1]
			sheet.Region = strings.Split(content["出差区域"].Value, common.SPLIT)[2]
		}
		sheet.Start_time, _ = time.ParseInLocation("2006-01-02 15:04:05", strings.Split(content["出差时间段"].Value, common.SPLIT)[0], time.Local)

		sheet.End_time, _ = time.ParseInLocation("2006-01-02 15:04:05", strings.Split(content["出差时间段"].Value, common.SPLIT)[1], time.Local)

		igc, _ := strconv.Atoi(strings.Split(content["面试官人数"].Value, common.SPLIT)[1])
		ibc, _ := strconv.Atoi(strings.Split(content["面试官人数"].Value, common.SPLIT)[0])
		agc, _ := strconv.Atoi(strings.Split(content["学生助手人数"].Value, common.SPLIT)[1])
		abc, _ := strconv.Atoi(strings.Split(content["学生助手人数"].Value, common.SPLIT)[0])

		sheet.Interviewer_girl_count = igc
		sheet.Interviewer_boy_count = ibc
		sheet.Assistant_girl_count = agc
		sheet.Assistant_boy_count = abc
		sheet.Attach = content["备注"].Value

		sheet.Apply_no = apply_no
		sheet.Interview_no = i_no

		//面试官
		for _, v := range interviewer {
			var tmp Interview_sheet_tbl
			tmp = sheet
			tmp.Person = v
			tmp.Person_type = 1

			err := orm.Create(&tmp).Error
			if err != nil {
				b = false
				i_no = ""
				return
			}
		}
		//助手
		for _, v := range assistant {
			var tmp Interview_sheet_tbl
			tmp = sheet
			tmp.Person = v
			tmp.Person_type = 2

			err := orm.Create(&tmp).Error
			if err != nil {
				b = false
				i_no = ""
				return
			}
		}

	}
	return
}
